package flc.ast.activity;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.cartoonavatarmaker.lib.core.AvatarColorType;
import com.stark.cartoonavatarmaker.lib.core.AvatarConst;
import com.stark.cartoonavatarmaker.lib.core.AvatarPartBean;
import com.stark.cartoonavatarmaker.lib.core.AvatarPartType;
import com.stark.cartoonavatarmaker.lib.core.AvatarRedoUndoController;
import com.stark.cartoonavatarmaker.lib.core.AvatarTemplatePart;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.adapter.AvatarTabAdapter;
import flc.ast.bean.AvatarTabBean;
import flc.ast.databinding.ActivityDressUpBinding;
import flc.ast.fragment.DressUpColorFragment;
import flc.ast.fragment.DressUpFragment;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class DressUpActivity extends BaseAc<ActivityDressUpBinding> {

    private List<Fragment> mFragments = new ArrayList<>();
    private List<String> mTitles = new ArrayList<>();
    private String mTmpShareFilePath;
    private static AvatarTemplatePart sTemplatePart;
    private AvatarTabAdapter tabAdapter;
    private int oldPosition;

    public static void start(@NonNull Context context, @Nullable AvatarTemplatePart templatePart) {
        Intent intent = IntentUtil.getIntent(context, DressUpActivity.class);
        sTemplatePart = templatePart;
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_dress_up;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDressUpBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSave.setOnClickListener(this);

        mDataBinding.rvTabList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        tabAdapter = new AvatarTabAdapter();
        mDataBinding.rvTabList.setAdapter(tabAdapter);
        tabAdapter.setOnItemClickListener(this);

        List<AvatarTabBean> list = new ArrayList<>();
        list.add(new AvatarTabBean(R.drawable.bjdk, R.drawable.bjgb, true));
        list.add(new AvatarTabBean(R.drawable.stdk, R.drawable.stgb, false));
        list.add(new AvatarTabBean(R.drawable.fsdk, R.drawable.fsgb, false));
        list.add(new AvatarTabBean(R.drawable.yfdk, R.drawable.yfgb, false));
        list.add(new AvatarTabBean(R.drawable.fsdk2, R.drawable.fsgb2, false));
        list.add(new AvatarTabBean(R.drawable.lgb, R.drawable.ldk, false));
        list.add(new AvatarTabBean(R.drawable.mrdk, R.drawable.mrgb, false));
        list.add(new AvatarTabBean(R.drawable.ykdk, R.drawable.ykgb, false));
        list.add(new AvatarTabBean(R.drawable.yqdk, R.drawable.yqgb, false));
        list.add(new AvatarTabBean(R.drawable.yqsdk, R.drawable.yqsgb, false));
        list.add(new AvatarTabBean(R.drawable.mmdk, R.drawable.mmgb, false));
        list.add(new AvatarTabBean(R.drawable.zbdk, R.drawable.zbgb, false));
        list.add(new AvatarTabBean(R.drawable.bzdk, R.drawable.bzgb, false));
        list.add(new AvatarTabBean(R.drawable.yjdk, R.drawable.yjgb, false));
        list.add(new AvatarTabBean(R.drawable.tfdk, R.drawable.tfgb, false));
        list.add(new AvatarTabBean(R.drawable.fsdk3, R.drawable.fsgb3, false));
        tabAdapter.setList(list);
        initFragments();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSave://保存
                getPermission();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        tabAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        tabAdapter.getItem(position).setSelected(true);
        tabAdapter.notifyDataSetChanged();
        FragmentUtils.showHide(position, mFragments);
    }

    @Override
    protected void initData() {
        mDataBinding.avatarMakeView.post(new Runnable() {
            @Override
            public void run() {
                if (sTemplatePart != null) {
                    mDataBinding.avatarMakeView.setTemplatePart(sTemplatePart);
                }
            }
        });
    }

    public void setAvatarPart(AvatarPartType type, AvatarPartBean partBean) {
        mDataBinding.avatarMakeView.setAvatarPart(type, partBean);
    }

    public void setColor(AvatarColorType type, @ColorInt int color) {
        mDataBinding.avatarMakeView.setColor(type, color);
    }

    private void initFragments() {
        mFragments.clear();
        mTitles.clear();
        for (AvatarPartType type : AvatarPartType.values()) {
            mFragments.add(DressUpFragment.newInstance(type));
            mTitles.add(AvatarConst.getAvatarPartTitle(type));
            if (type == AvatarPartType.body) {
                mFragments.add(DressUpColorFragment.newInstance(AvatarColorType.SKIN));
                mTitles.add(getString(R.string.cam_skin_color));
            } else if (type == AvatarPartType.eyeball) {
                mFragments.add(DressUpColorFragment.newInstance(AvatarColorType.EYE_BALL));
                mTitles.add(getString(R.string.cam_eyeball_color));
            } else if (type == AvatarPartType.hair) {
                mFragments.add(DressUpColorFragment.newInstance(AvatarColorType.HAIR));
                mTitles.add(getString(R.string.cam_hair_color));
            }
        }
        FragmentUtils.add(getSupportFragmentManager(), mFragments, R.id.fl_container, 0);
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO).reqPermissionDesc(getString(R.string.get_save_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                save();
            }
        }).request();
    }

    private void save() {
        showDialog(getString(com.stark.common.res.R.string.saving));
        Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.avatarMakeView);
        RxUtil.create(new RxUtil.Callback<Boolean>() {
            @Override
            public void doBackground(ObservableEmitter<Boolean> emitter) {
                File file = ImageUtils.save2Album(bitmap, Bitmap.CompressFormat.PNG);
                //保存到记录
                String resultFile = FileUtil.generateFilePath(Content.myRecord, ".png");
                ImageUtils.save(bitmap, resultFile, Bitmap.CompressFormat.PNG);
                emitter.onNext(true);
            }

            @Override
            public void accept(Boolean save) {
                int msgId = com.stark.common.res.R.string.save_failure;
                if (save) {
                    msgId = R.string.save_to_album;
                }
                ToastUtils.showShort(msgId);
                dismissDialog();
                startActivity(HomeActivity.class);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(mTmpShareFilePath)) {
            FileUtils.delete(mTmpShareFilePath);
            mTmpShareFilePath = null;
        }
    }

}